package com.eat.fish_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eat.common_utils.IpAndPortUtil;
import com.eat.common_utils.R;
import com.eat.fish_service.entity.Dao.DiseasesEncyclopedia;
import com.eat.fish_service.entity.DiseaseQuery;
import com.eat.fish_service.mapper.DiseasesEncyclopediaMapper;
import com.eat.fish_service.service.DiseasesEncyclopediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LWB
 * @since 2022-04-29
 */
@Service
public class DiseasesEncyclopediaServiceImpl extends ServiceImpl<DiseasesEncyclopediaMapper, DiseasesEncyclopedia> implements DiseasesEncyclopediaService {

    @Value("${uploadDiseasePath}")
    String uploadPath;

    @Autowired
    private DiseasesEncyclopediaService diseasesEncyclopediaService;

    @Override
    public R addFishDiseaseBaiKePic(MultipartFile pic) {
        if (pic != null) {
            String fileName = System.currentTimeMillis() + pic.getOriginalFilename();

            //第一次运行的时候，这个文件所在的目录往往是不存在的，这里需要创建一下目录
            File dir = new File(uploadPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            //把浏览器上传的文件复制到希望的位置
            File targetFile = new File(uploadPath, fileName);
            try {
                pic.transferTo(targetFile);
            } catch (IOException e) {
                e.printStackTrace();
                return R.error().message("上传失败");
            }

            //服务器的ip和端口号
            String ipAndPort = IpAndPortUtil.getIpAndPort();
            String picUrl = "http://" + ipAndPort + "/getDiseaseBaiKe/" + fileName;

            //返回获取图片的url
            return R.ok().data("picUrl", picUrl);

        } else {
            return R.error().message("上传的文件为空！");
        }
    }

    @Override
    public R addFishDiseaseBaiKe(String fishdiseaseName, String introduction, String fishDiseaseImg, String author) {
        DiseasesEncyclopedia diseasesEncyclopedia = new DiseasesEncyclopedia();

        diseasesEncyclopedia.setDiseaseName(fishdiseaseName);
        diseasesEncyclopedia.setIntroduction(introduction);
        diseasesEncyclopedia.setDiseaseImg(fishDiseaseImg);
        diseasesEncyclopedia.setAuthor(author);
        diseasesEncyclopedia.setIsDeleted(0);

        diseasesEncyclopedia.setGmtCreate(new Date());
        diseasesEncyclopedia.setGmtModified(new Date());


        int insert = baseMapper.insert(diseasesEncyclopedia);

        if (insert == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @Override
    public R pageFishDiseaseCondition(Long current, Long size, DiseaseQuery diseaseQuery) {
        //创建page
        Page<DiseasesEncyclopedia> pageFishDiseases = new Page<>(current, size);
        //构建条件
        QueryWrapper<DiseasesEncyclopedia> wrapper = new QueryWrapper<>();
        //多条件查询
        String diseaseName = diseaseQuery.getDiseaseName();
        String author = diseaseQuery.getAuthor();
        String TimeBegin = diseaseQuery.getTimeBegin();
        String TimeEnd = diseaseQuery.getTimeEnd();
        String introduction = diseaseQuery.getIntroduction();


        wrapper.eq("is_deleted", 0);
        if (!StringUtils.isEmpty(diseaseName)) {
            wrapper.eq("disease_name", diseaseName);
        }
        if (!StringUtils.isEmpty(author)) {
            wrapper.eq("author", author);
        }
        if (!StringUtils.isEmpty(introduction)) {
            wrapper.like("introduction", introduction);
        }
        if (!StringUtils.isEmpty(TimeBegin)) {
            wrapper.ge("gmt_create", TimeBegin);
        }
        if (!StringUtils.isEmpty(TimeEnd)) {
            wrapper.le("gmt_create", TimeEnd);
        }

        //调用方法实现查询
        diseasesEncyclopediaService.page(pageFishDiseases, wrapper);

        long total = pageFishDiseases.getTotal();//获取总记录数
        List<DiseasesEncyclopedia> records = pageFishDiseases.getRecords();//获取分页后的list集合
        long pages = pageFishDiseases.getPages();

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        if (current > pages) {
            return R.error().code(13).message("超过总页数");
        } else {
            return R.ok().data(map).data("maxPages",pages);
        }
    }

    @Override
    public R getFishDisease(String fishDiseaseId) {

        DiseasesEncyclopedia diseasesEncyclopedia = diseasesEncyclopediaService.getById(fishDiseaseId);

        return R.ok().data("diseasesEncyclopedia", diseasesEncyclopedia);
    }

    @Override
    public R getAllDiseases() {
        List<DiseasesEncyclopedia> list = baseMapper.selectList(null);
        ArrayList<String> diseases = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            diseases.add(list.get(i).getDiseaseName());
        }
        return R.ok().data("diseases", diseases);
    }
}

